﻿//using System.Collections.Generic;
//using UnityEngine;
//using UnityEngine.Events;
//#if  UNITY_IOS
//using System.Runtime.InteropServices;
//#endif
//namespace QQSDK
//{

//	public class KKPurchase : MonoBehaviour
//    {
//		AndroidJavaObject KKPurchaseManager;
//#if UNITY_EDITOR || DEVELOP

//#elif UNITY_IOS
//        //[DllImport("__Internal")]
//        //private static extern void initPurchase(bool debuglog,string infos);
//        //[DllImport("__Internal")]
//        //private static extern void buyProduct( string productIdentifier);
//        //[DllImport("__Internal")]
//        //private static extern void refreshSubsribtionState();
//#elif UNITY_ANDROID
//           //AndroidJavaObject KKPurchaseManager;
//#endif
//        public static KKPurchase instance;
//		public UnityAction<PurchaseState,ProductType> onFetchProductionDetailComplete;
//		public UnityAction<string> onRestore;
//        public UnityAction<PurchaseState,string> onPurchaseCallback;
//        public bool showLog;
//        public string BASE_64_ENCODED_PUBLIC_KEY;
//        private bool isInit;
//        public enum PurchaseState
//        {
//            FetchFailed,
//			FetchSucceed,
//			PurchasedSucceed,
//			PurchasedFailed,
//            Restore,
//            Cancel
//        }
//        private List<ProductDetail> allProduct=new List<ProductDetail>();
//        private void Awake()
//        {
//            if (instance == null)
//            {
//                instance = this;
//                DontDestroyOnLoad(this);
//            }
//        }
//        private void Log(object obj)
//        {
//            if (showLog)
//                SDKFunc.LogError(obj.ToString());
//        }

//        public void Init(bool showlog, List<ProductDetail> products)
//        {
//            this.showLog = showlog;
//            List<string> jsonString = new List<string>();
//            for (int i = 0; i < products.Count; i++)
//            {
//                jsonString.Add(JsonUtil.toJson(products[i]));
//            }

//            string productJson = JsonUtil.toJson(products);
//            Log(productJson);
//#if UNITY_EDITOR || DEVELOP
//#elif UNITY_IOS
//#elif UNITY_ANDROID
//            KKPurchaseManager = new AndroidJavaClass("com.tapque.kkpay.KKPurchaseManager").CallStatic<AndroidJavaObject>("instance");
//            KKPurchaseManager.Call("init", CommomUtils.currentlyActivity, BASE_64_ENCODED_PUBLIC_KEY, this.showLog, productJson);
//#endif
//        }
//        public void RefreshSubsribtionState()
//        {

//        }
//        //private UnityAction<string> onPurchaseSucceed;
//        //private UnityAction<string> onPurchaseFailed;
//        public void BuyProduct(ProductDetail productDetail,bool blockScreen=true)
//        {
//            string productJson = JsonUtil.toJson(productDetail);
//            Log("购买的商品:" + productJson);
//            if(KKPurchaseManager!=null)
//            KKPurchaseManager.Call("buyProduct", productJson);
//#if UNITY_EDITOR || DEVELOP

//#elif UNITY_IOS
       
//#elif UNITY_ANDROID
         
//#endif


//        }
//        public ProductDetail QueryProductInfo(string productIdentifier)
//        {
//            if (allProduct.Count <=0||!isInit)
//            {
//                SDKFunc.LogError("请先InitPurchase，进行查询所有信息后在进行查询条目信息");
//                return null;
//            }
//            else
//            {
//                return allProduct.Find(x => x.productIdentifier.Equals(productIdentifier));
//            }
//        }
//        List<object> lists;
//        private void Listener(string s)
//        {
//            Dictionary<string, object> dic = Json.Deserialize(s) as Dictionary<string, object>;
//            string dataString = dic[PurchaseConstant.DATA].ToString();
//            switch (dic[PurchaseConstant.ACTION].ToString())
//            {
//                case PurchaseConstant.QUERY_FAILED://抓取商品失败
//                    onFetchProductionDetailComplete?.Invoke(PurchaseState.FetchFailed,ProductType.Consume);
//                    break;
//                case PurchaseConstant.FETCH://抓取商品信息
//                    lists = Json.Deserialize(dataString) as List<object>;
                   
//                    ProductType productType = ProductType.Consume;
//                    foreach (var item in lists)
//                    {
//                        Dictionary<string, object> infos = item as Dictionary<string, object>;
//                        string productIdentifier = (string)infos["productIdentifier"];
//                        string localizedDescription = (string)infos["localizedDescription"];
//                        string localizedTitle = (string)infos["localizedTitle"];
//                        string priceCurrencyCode = (string)infos["priceCurrencyCode"];
//                        string price = (string)infos["price"];
//                        long purchasetype = (long)infos["purchasetype"];
//                        productType = (ProductType)purchasetype;
//                        ProductDetail productDetail = new ProductDetail(productIdentifier, localizedDescription, localizedTitle, price, (ProductType)purchasetype);
//                        if (!allProduct.Contains(productDetail))
//                        {
//                            allProduct.Add(productDetail);
//                        }
                       
//                    }
//                    isInit = true;
//                    onFetchProductionDetailComplete?.Invoke(PurchaseState.FetchSucceed, productType);
//                    break;
//                case PurchaseConstant.PURCHASE_RESTORE://恢复购买
//                    onRestore?.Invoke(dataString);
//                    break;
//                case PurchaseConstant.PURCHASE_SUCCEED://购买成功
//                    onPurchaseCallback?.Invoke(PurchaseState.PurchasedSucceed,dataString);
//                    break;
//                case PurchaseConstant.PURCHASE_FAILED://购买失败
//                    onPurchaseCallback?.Invoke(PurchaseState.PurchasedFailed, dataString);
//                    break;
//            }
//        }
//        private void OnApplicationFocus(bool focus)
//        {
//            if (focus)
//            {
//                //SDKFunc.LogError("进入焦点");
//                RefreshSubsribtionState();
//            }
//            else
//            {
//                //SDKFunc.LogError("失去焦点");
//            }
//        }
//    }

//}


